package com.qst.itoffer.service;

import com.qst.itoffer.entity.Company;
import com.qst.itoffer.mapper.CompanyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
@Service
public class CompanyService {
    @Autowired
    private CompanyMapper companyMapper;

    //查询企业
    public List<Company> getAllCompanies() {
        return companyMapper.selectAll();
    }

    //通过id查询单个企业
    public Company getCompanyById(Integer companyId) {
        return companyMapper.selectById(companyId);
    }
    //修改企业
    public void updateCompany(Company company) {
        companyMapper.update(company);
    }//文件
    public void save(Company company, MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            // 生成唯一文件名
            String originalFilename = file.getOriginalFilename();
            String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFileName = UUID.randomUUID().toString() + fileExtension;

            // 保存文件
            File uploadPath = new File("/static" + "/companyPic");
            if (!uploadPath.exists()) {
                uploadPath.mkdirs();
            }

            File dest = new File(uploadPath, newFileName);
            file.transferTo(dest);

            company.setCompanyPic(newFileName);
        }
        //添加公司信息
        companyMapper.insert(company);
    }

    public void deleteCompany(Integer companyId) {
        companyMapper.delete(companyId);
    }
}


